




#include <cmath>

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "piechartpoints.h"
#endif

#include <wx/wxprec.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include "wx/label.h"
#include "wx/piechartpoints.h"
#include "wx/chartsizes.h"



wxPieChartPoints::wxPieChartPoints(
	wxString name,
	ChartColor c,
    bool showlabel
) : wxChartPoints(wxChartPointsTypes::Pie()),
	m_Name(name),
	m_Color(c),
	m_Zoom(1),
	m_PieTag(NONE),
    m_ShowLabel(showlabel) {
}


//----------------------------------------------------------------------E-+++
ChartValue wxPieChartPoints::GetXVal(
	int n
) const {
	return ( m_Points.GetXVal(n) );
}


//----------------------------------------------------------------------E-+++
ChartValue wxPieChartPoints::GetYVal(
	int n
) const {
	return ( m_Points.GetYVal(n) );
}


//----------------------------------------------------------------------E-+++
wxString wxPieChartPoints::GetName(
	int n
) const {
	return ( m_Points.GetName(n) );
}


//----------------------------------------------------------------------E-+++
ChartColor wxPieChartPoints::GetColor(int n) const {
	return ( m_Points.GetColor(n) );
}


//----------------------------------------------------------------------E-+++
int wxPieChartPoints::GetCount() const {
	return ( m_Points.GetCount() );
}


//----------------------------------------------------------------------E-+++
ChartValue wxPieChartPoints::GetMaxX() const {

	return ( 0 );
}


//----------------------------------------------------------------------E-+++
ChartValue wxPieChartPoints::GetMaxY() const {
//	return ( m_Points.GetMaxY() );
	return ( 0 );
}


//----------------------------------------------------------------------E-+++
ChartValue wxPieChartPoints::GetMinX() const {
//	return ( m_Points.GetMinX() );
	return ( 0 );
}


//----------------------------------------------------------------------E-+++
ChartValue wxPieChartPoints::GetMinY() const {
//	return ( m_Points.GetMinY() );
	return ( 0 );
}


void wxPieChartPoints::SetZoom(
	double z
) {
	m_Zoom = z;
}


//----------------------------------------------------------------------E-+++
double wxPieChartPoints::GetZoom() {
	return ( m_Zoom );
}


void wxPieChartPoints::SetSizes(
	wxChartSizes *sizes
) {
	m_Sizes = sizes;
}


//----------------------------------------------------------------------E-+++
wxChartSizes* wxPieChartPoints::GetSizes() const {
	return ( m_Sizes );
}


//----------------------------------------------------------------------E-+++
ChartColor wxPieChartPoints::GetColor() const {
	return ( m_Color );
}


void wxPieChartPoints::SetColor(
	ChartColor c
) {
	m_Color = c;
}


//----------------------------------------------------------------------E-+++
wxString wxPieChartPoints::GetName() const {
	return ( m_Name );
}


void wxPieChartPoints::SetName(
	wxString name
) {
	m_Name =  name;
}



void wxPieChartPoints::Add(
	wxString name,
	ChartValue x,
	ChartValue y
) {
	Add( name, x, y, wxCHART_NOCOLOR );
}



void wxPieChartPoints::Add(
	wxString name,
	ChartValue x,
	ChartValue y,
	ChartColor c
) {
	if ( c == wxCHART_NOCOLOR )
		c = wxChartColors::GetColor();
	m_Points.Add( name, x, y, c );
}


void wxPieChartPoints::SetDisplayTag(
 	wxDISPLAY_LABEL d
) {
	m_PieTag = d;
}


//----------------------------------------------------------------------E-+++
wxDISPLAY_LABEL wxPieChartPoints::GetDisplayTag() const {
	return ( m_PieTag );
}



void wxPieChartPoints::Draw(
    CHART_HPAINT hp,
    CHART_HRECT hr
) {

   
   
   
    wxChartSizes *sizes = GetSizes();

    int r = (int)wxMin( (int)hr->w/2,
        (int)(hr->h - 2*sizes->GetSizeHeight())/2 );

    if ( r > 0 )
    {
        int iNodes = GetCount();

        if ( iNodes > 0 )
        {
            int iData;
            int ValTot;
            int iDatas = GetCount();
            for ( iData = 0, ValTot = 0; iData < iDatas; ++ iData )
                ValTot += static_cast<int>(GetYVal( iData ));

            double percent;
            double grad;
            double rad;
            int x1, y1, x2, y2, xc, yc;
            xc = hr->x + hr->w/2;
            yc = hr->y + hr->h/2;
            x1 = xc + r;
            y1 = yc;

            hp->SetPen( *wxBLACK_PEN );

            for ( iData = 0, rad = 0; iData < iDatas; ++ iData )
            {
                hp->SetBrush( wxBrush(GetColor(iData), wxSOLID) );

               
                percent = (double)(GetYVal(iData) * 100) / (double)ValTot;
                grad    = (double)(percent * 360) / (double)100;
                rad     += (double)(grad * 3.1415) / (double)180;

                x2 = (int)(xc + r * cos( rad ));
                y2 = (int)(yc - r * sin( rad ));
                hp->DrawArc( x1, y1, x2, y2, xc, yc );
                x1 = x2;
                y1 = y2;

               
               
               
                if (!m_ShowLabel)
                    continue;

                wxString lbl;
                wxLabel wxLbl;

                LABEL_POSITION p;
                if ( x2 > xc )
                    p = RIGHT;
                else
                    p = LEFT;
                if ( y2 > yc )
                    p = (LABEL_POSITION)( p | DOWN );
                else
                    p = (LABEL_POSITION)( p | UP );

                switch ( GetDisplayTag() )
                {
                case XVALUE:
                    lbl.Printf( wxT("%d"), static_cast<int>(GetXVal(iData)) );
                    wxLbl.Draw( hp, x2, y2, GetColor(iData), lbl, p );
                    break;
                case YVALUE:
                    lbl.Printf( wxT("%d"), static_cast<int>(GetYVal(iData)) );
                    wxLbl.Draw( hp, x2, y2, GetColor(iData), lbl, p );
                    break;
                case XVALUE_FLOAT:
                    lbl.Printf( wxT("%4.1f"), GetXVal(iData) );
                    wxLbl.Draw( hp, x2, y2, GetColor(iData), lbl, p );
                    break;
                case YVALUE_FLOAT:
                    lbl.Printf( wxT("%4.1f"), GetYVal(iData) );
                    wxLbl.Draw( hp, x2, y2, GetColor(iData), lbl, p );
                    break;
                    case NAME:
                    lbl = GetName(iData).c_str();
                    wxLbl.Draw( hp, x2, y2, GetColor(iData), lbl, p );
                    break;
                case NONE:
                default:
                    break;
                }
            }
        }
    }

}



wxPieChartPoints* wxPieChartPoints::CreateWxPieChartPoints(
    wxString name,
    ChartColor c,
    bool showlabel
) {
    if ( c == wxCHART_NOCOLOR )
        c = wxChartColors::GetColor();

    return new wxPieChartPoints( name, c, showlabel );
}
